<template>
	<view>
		<view class="PolicySearch Enterprise">
			<view class="PolicySearchc">
				<input type="text" v-model="searchKeyword" placeholder="输入企业名称">
				<view class="PolocyBtn" @click="handleSearch">搜索</view>
			</view>
		</view>
		<view class="EnterpriseContainer">
			<view class="EnterpriseTitle" @click="isShow = true">关联新企业</view>
			<view class="EnterpriseList">
				<view class="EnterpriseLi" v-for="(item, index) in companyList" :key="index">
					<view class="EnterpriseLiFixed" v-if="item.defaultFlag === 1">默认企业</view>
					<view class="EnterpriseLis" @click="handleViewDetail(item.companyCreditCode)">
						<view class="EnterpriseLiTitle">
							<view class="title-content" @click="handleViewDetail(item.companyCreditCode)">
								{{ !eyeStates[item.id] ? hideCompanyName(item.companyName) : item.companyName }}
							</view>
							<view class="eyeImg" @click.stop.prevent="toggleEyeState(item.id)">
								<image v-if="eyeStates[item.id]" class="eyeOn" src="/static/images/yanjingon1.png"></image>
								<image v-else class="eyeOff" src="/static/images/yanjingoff1.png"></image>
							</view>
						</view>
						<view class="EnterpriseLiText">企业名称：{{item.companyName}}</view>
						<view class="EnterpriseLiText">统一社会代码：{{item.companyCreditCode}}</view>
					</view>
					<view class="EnterpriseLix">
						<view class="EnterpriseLixBtn" v-if="item.defaultFlag !== 1" @click="handleDelete(item.id)">取消关联</view>
						<view class="EnterpriseLixBtn EnterpriseLixBtnYes" v-if="item.defaultFlag !== 1" @click="handleSetDefault(item.id)">设置默认企业</view>
					</view>
				</view>
			</view>
		</view>
		<association :isShow="isShow" @changeAssociation="changeAssociation" @refreshList="getCompanyList" />
	</view>
</template>

<script>
	import association from '@/pages/components/association.vue'
	import { getCompanyList, setDefaultCompany, deleteCompany } from '@/api/api.js'
	export default {
		components: { association },
		data() {
			return {
				isShow: false,
				companyList: [],
				searchKeyword: '',
				eyeStates: {} // 用于存储每个企业的显示状态
			};
		},
		watch: {
			searchKeyword: {
				handler(newVal) {
					this.getCompanyList()
				},
				delay: 500
			}
		},
		created() {
			this.getCompanyList()
		},
		methods: {
			toggleEyeState(id) {
				this.$set(this.eyeStates, id, !this.eyeStates[id]);
			},
			hideCompanyName(name) {
				return name.replace(/^(.{2})(.*)(.{3})$/, (match, p1, p2, p3) => {
			    return p1 + '*'.repeat(p2.length) + p3;
			  });
			},
			handleViewDetail(companyCode) {
				uni.navigateTo({
					url: `/pages/enterprise/details/index?creditCode=${companyCode}`
				})
			},
			handleSearch() {
				this.getCompanyList()
			},
			changeAssociation() {
				this.isShow = false
			},
			async handleSetDefault(id) {
				try {
					const result = await setDefaultCompany(id)
					if (result.code === 0) {
						uni.showToast({
							title: '设置成功',
							icon: 'success'
						})
						const eventChannel = this.getOpenerEventChannel()
						eventChannel.emit('refreshData')
						await this.getCompanyList()
					} else {
						uni.showToast({
							title: result.message || '设置失败',
							icon: 'none'
						})
					}
				} catch (error) {
					console.error('设置默认企业失败：', error)
					uni.showToast({
						title: '设置失败',
						icon: 'none'
					})
				}
			},
			async getCompanyList() {
				try {
					const result = await getCompanyList({
						companyName: this.searchKeyword
					})
					console.log('企业列表数据：', result)
					if (result.code === 0) {
						this.companyList = result.data?.list || []
					} else {
						console.error('获取企业列表失败：', result.message)
					}
				} catch (error) {
					console.error('获取企业列表失败：', error)
				}
			},
			async handleDelete(id) {
				try {
					const result = await deleteCompany(id)
					if (result.code === 0) {
						uni.showToast({
							title: '取消关联成功',
							icon: 'success'
						})
						await this.getCompanyList()
					} else {
						uni.showToast({
							title: result.message || '取消关联失败',
							icon: 'none'
						})
					}
				} catch (error) {
					console.error('取消关联失败：', error)
					uni.showToast({
						title: '取消关联失败',
						icon: 'none'
					})
				}
			}
		}
	}
</script>

<style lang="scss">
page{
	background: #f7f8fc;
}
.Enterprise{
	padding: 30rpx 24rpx 40rpx;
	background: none;
}
.EnterpriseContainer{
	padding: 0 24rpx;
	.EnterpriseTitle{
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		font-size: 32rpx;
		color: #ffffff;
		background: #2f59fe;
		border-radius: 10rpx;
	}
	.EnterpriseList{
		padding: 30rpx 0 0;
		.EnterpriseLi{
			margin: 0 0 29rpx;
			padding: 38rpx 30rpx 30rpx;
			background: #ffffff;
			border-radius: 20rpx;
			position: relative;
			.EnterpriseLiFixed{
				position: absolute;
				right: 27rpx;
				top: -14rpx;
				width: 184rpx;
				height: 49rpx;
				line-height: 49rpx;
				padding-left: 30rpx;
				box-sizing: border-box;
				font-size: 26rpx;
				color: #ffffff;
				background: url("/static/images/morenbj.png") no-repeat center / 184rpx 49rpx;
			}
			.EnterpriseLis{
				padding: 0 0 20rpx;
				border-bottom: 1px solid #f1f1f1;
				.EnterpriseLiTitle{
					font-size: 32rpx;
					color: #202020;
					padding: 0 0 12rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					.title-content {
						flex: 1;
						margin-right: 20rpx;
						line-height: 1;
					}
					.eyeImg {
						display: flex;
						align-items: center;
						justify-content: center;
						width: 50rpx;
						height: 40rpx;
						padding: 10rpx;
						box-sizing: border-box;
						margin-top: -4rpx;
					
						.eyeOn {
							width: 22rpx;
							height: 17rpx;
						}
					
						.eyeOff {
							width: 19rpx;
							height: 7rpx;
						}
					}
				}
				.EnterpriseLiText{
					font-size: 28rpx;
					color: #4e5262;
					line-height: 55rpx;
				}
			}
			.EnterpriseLix{
				display: flex;
				justify-content: flex-end;
				padding: 29rpx 0 0;
				.EnterpriseLixBtn{
					height: 60rpx;
					line-height: 58rpx;
					font-size: 28rpx;
					color: #202020;
					padding: 0 29rpx;
					border: 1px solid #cfd2d7;
					border-radius: 10rpx;
					margin-left: 20rpx;
					&.EnterpriseLixBtnYes{
						color: #2f59fe;
						border-color: #3e65fe;
					}
				}
			}
		}
	}
}
// <view class="EnterpriseList">
// 				<view class="EnterpriseLi">
// 					<view class="EnterpriseLiFixed">默认企业</view>
// 					<view class="EnterpriseLis">
// 						<view class="EnterpriseLiTitle">
// 							内蒙古******有限公司
// 							<view class="eyeImg" @click="eyeType = !eyeType">
// 								<image v-if="eyeType" class="eyeOn" src="/static/images/yanjingon.png"></image>
// 								<image v-else class="eyeOff" src="/static/images/yanjingoff.png"></image>
// 							</view>
// 						</view>
// 						<view class="EnterpriseLiText">统一社会代码：9115***********25Y</view>
// 						<view class="EnterpriseLiText">法定代表人：高*</view>
// 					</view>
// 					<view class="EnterpriseLix">
// 						<view class="EnterpriseLixBtn" v-if="item.defaultFlag !== 1" @click="handleDelete(item.id)">取消关联</view>
// 						<view class="EnterpriseLixBtn EnterpriseLixBtnYes">设置默认企业</view>
// 					</view>
// 				</view>
// 			</view>
</style>
